/**
 * Created by yb on 2018/6/11.
 * 时间格式化
 */
//时间转换函数
function transferTimeShort(obj) {
  var date = new Date(obj);
  var create_year = date.getFullYear();
  var create_month = date.getMonth() + 1;
  var create_day = date.getDate();
  var create_hour = date.getHours();
  var create_minute = date.getMinutes();
  var create_second = date.getSeconds();
  var date_str = toDouble(create_year) + "-" + toDouble(create_month) + "-" + toDouble(create_day);
  return date_str;
}

function transferTime(obj) {
  var date = new Date(obj);
  var create_year = date.getFullYear();
  var create_month = date.getMonth() + 1;
  var create_day = date.getDate();
  var create_hour = date.getHours();
  var create_minute = date.getMinutes();
  var create_second = date.getSeconds();
  var date_str = toDouble(create_year) + "-" + toDouble(create_month) + "-" + toDouble(create_day) + " " + toDouble(create_hour) + ":" + toDouble(create_minute) + ":" + toDouble(create_second);
  // var date_str = toDouble(create_year) + "-" + toDouble(create_month) + "-" + toDouble(create_day);
  return date_str;
}

function toDouble(str) {
  var obj = str.toString();
  if (obj.length == 1) {
    return "" + 0 + obj;
  } else {
    return obj;
  }
}

// 时间距离 （多少分钟前）
function timeAgo(valueTime) {
  if (valueTime) {
    valueTime = Date.parse(valueTime)
    var newData = Date.parse(new Date());
    var diffTime = Math.abs(newData - valueTime);
    if (diffTime > 7 * 24 * 3600 * 1000) {
      var date = new Date(valueTime);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      var h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      var minute = date.getMinutes();
      var second = date.getSeconds();
      minute = minute < 10 ? ('1' + minute) : minute;
      second = second < 10 ? ('0' + second) : second;
      return m + '-' + d + ' ' + h + ':' + minute;

    } else if (diffTime < 7 * 24 * 3600 * 1000 && diffTime > 24 * 3600 * 1000) {
      // 一周之内;
      // var time = newData - diffTime;
      var dayNum = Math.floor(diffTime / (24 * 60 * 60 * 1000));
      return dayNum + "天前";

    } else if (diffTime < 24 * 3600 * 1000 && diffTime > 3600 * 1000) {
      // 一天之内";
      // var time = newData - diffTime;
      var dayNum = Math.floor(diffTime / (60 * 60 * 1000));
      return dayNum + "小时前";

    } else if (diffTime < 3600 * 1000 && diffTime > 0) {
      // 一小时之内;
      // var time = newData - diffTime;
      var dayNum = Math.floor(diffTime / (60 * 1000));
      return dayNum + "分钟前";

    }
  }
}


export {
  transferTimeShort,
  transferTime,
  fomatTime,
  timeAgo
}
